const xml2js = require('xml2js');

// 解析XML到JSON
function parseXML(xml) {
  return new Promise((resolve, reject) => {
    const parser = new xml2js.Parser({
      explicitArray: false,
      explicitRoot: false
    });
    
    parser.parseString(xml, (err, result) => {
      if (err) reject(err);
      else resolve(result);
    });
  });
}

// 将JSON转换为XML
function buildXML(json) {
  const builder = new xml2js.Builder({
    rootName: 'xml',
    cdata: true,
    headless: true
  });
  
  return builder.buildObject(json);
}

module.exports = {
  parseXML,
  buildXML
};